<?php


namespace app\admin\controller;


use app\model\Nav;
use think\Controller;

class Setting extends Allow
{
    /**
     * 站点设置
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $setting = db("setting")->find();
        $this->assign('setting',$setting);
        return view("index");
    }

    /**
     * 站点修改
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function executeData()
    {
        if(request()->post()){
            $data = request()->except('id');
            $id = request()->id;
            $setting = db("setting")->find($id);
//            $data['index_image'] = editImage($data,$setting,'index_image');
            $data['index_logo'] = editImage($data,$setting,'index_logo');
            $data['wxgzh'] = editImage($data,$setting,'wxgzh');
            $data['app'] = editImage($data,$setting,'app');
            $data['wxxcx'] = editImage($data,$setting,'wxxcx');
            $data['wb'] = editImage($data,$setting,'wb');
            $data['qq'] = editImage($data,$setting,'qq');
            $data['wx'] = editImage($data,$setting,'wx');
            $res = db("setting")->where('id',$id)->update($data);
            if(!$res) return errorMsg('更新失败');
            return successMsg('更新成功');
        }
    }

    /**
     * 导航列表
     * @return \think\response\View
     */
    public function nav()
    {
        return view('nav',['data' => Nav::all()]);
    }

    /**
     * 导航修改页面
     * @return \think\response\View
     */
    public function edit_nav()
    {
        return view("edit_nav",['data' => Nav::get(input("id"))]);
    }

    /**
     * 导航添加页面
     * @return \think\response\View
     */
    public function add_nav(){
        return view("add_nav");
    }

    /**
     * 执行添加修改
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function executeDataNav()
    {
        $param = request()->param();
        if(empty($param['id'])){
            $data = [
                'title' => $param['title'],
                'image' => fileImage('image'),
            ];
            $res = (new Nav())->insert($data);
            if(!$res) return errorMsg('添加失败');
            return successMsg('添加成功');
        }else{
            $old_data = db('nav')->find($param['id']);
            $data = [
              'title' => $param['title'],
              'image' => editImage($param,$old_data,'image'),
            ];
            $res = (new Nav())->save($data,['id' => $param['id']]);
            if(!$res) return errorMsg('修改失败');
            fileDel('.'.$old_data['image']);//删除老图片
            return successMsg('修改成功');
        }
    }

    /**
     * 执行删除
     * @param $id
     * @return array
     * @throws \Exception
     */
    public function delete_nav($id)
    {
        $obj = (new Nav());
        $data = $obj->get($id);
        fileDel('.'.$data['image']);
        $obj->where('id',$id)->delete();
        return successMsg('删除成功');
    }


}